﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AVCare.LMS.LocalData;
using AVCare.LMS.DataAccess;
using AVCare.LMS.Models;
using System.Data.Linq;

namespace AVCare.LMS.LocalData
{
    public class MenuHelper : IMenuHelper
    {
        LMSEntities entities = new LMSEntities();

        #region IMenuHelper 成员

        public string GetMenuName(string ID)
        {
            try
            {
                string fName = string.Empty;
                var x = from d in entities.Functions where d.ID == ID select new { fName = d.Name };

                return fName;
            }
            catch
            {
                throw;
            }


        }

        public List<Function> GetMenus(params string[] roleID)
        {
            /** 根据用户ID获取用户权限
            var query = from u in entities.Users
                        join r in entities.UserRoles
                        on u.ID equals r.UserID
                        into roles
                        where u.ID == userID
                        from x in roles.DefaultIfEmpty()
                        join rf in entities.RoleFunction
                        on x.RoleID equals rf.RoleID
                        into fid
                        from f in fid.DefaultIfEmpty()
                        join fs in entities.Functions
                        on f.FunctionID equals fs.ID
                        select fs;
            */
            var query = from r in entities.RoleFunction
                        join f in entities.Functions
                        on r.FunctionID equals f.ID
                        where roleID.Contains(r.RoleID) && f.ParnetID == "LMS000"
                        select f;

            return query.ToList();

        }

        public List<Function> GetSubMenus(string parentID, params string[] roleID)
        {
            var query = from r in entities.RoleFunction
                        join f in entities.Functions
                        on r.FunctionID equals f.ID
                        where roleID.Contains(r.RoleID) && f.ParnetID == parentID
                        select f;

            return query.ToList();
        }

        #endregion
    }
}
